using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class DebtTodayDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public DebtTodayDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the DebtToday table.
		/// </summary>
		public virtual void Insert(DebtTodayInfo debtTodayInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdDebt", debtTodayInfo.IdDebt),
				new SqlParameter("@PostDate", debtTodayInfo.PostDate),
				new SqlParameter("@IdCommon", debtTodayInfo.IdCommon),
				new SqlParameter("@AccountNo", debtTodayInfo.AccountNo),
				new SqlParameter("@Debt", debtTodayInfo.Debt)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "DebtToday_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the DebtToday table.
		/// </summary>
		public virtual void Update(DebtTodayInfo debtTodayInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdDebt", debtTodayInfo.IdDebt),
				new SqlParameter("@PostDate", debtTodayInfo.PostDate),
				new SqlParameter("@IdCommon", debtTodayInfo.IdCommon),
				new SqlParameter("@AccountNo", debtTodayInfo.AccountNo),
				new SqlParameter("@Debt", debtTodayInfo.Debt)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "DebtToday_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the DebtToday table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idDebt)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdDebt", idDebt)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "DebtToday_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the DebtToday table.
		/// </summary>
		public virtual DebtTodayInfo Select(Guid? idDebt)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdDebt", idDebt)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "DebtToday_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeDebtTodayInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the DebtToday table.
		/// </summary>
		public List<DebtTodayInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "DebtToday_SelectAll"))
			{
				List<DebtTodayInfo> debtTodayInfoList = new List<DebtTodayInfo>();
				while (dataReader.Read())
				{
					debtTodayInfoList.Add(MakeDebtTodayInfo(dataReader));
				}

				return debtTodayInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the DebtToday class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual DebtTodayInfo MakeDebtTodayInfo(SqlDataReader dataReader)
		{
			DebtTodayInfo debtTodayInfo = new DebtTodayInfo();
			debtTodayInfo.IdDebt = SqlClientUtility.GetGuid(dataReader, DbConstants.DEBTTODAY.IDDEBT);
			debtTodayInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.DEBTTODAY.POSTDATE);
			debtTodayInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.DEBTTODAY.IDCOMMON);
			debtTodayInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.DEBTTODAY.ACCOUNTNO);
			debtTodayInfo.Debt = SqlClientUtility.GetDecimal(dataReader, DbConstants.DEBTTODAY.DEBT);

			return debtTodayInfo;
		}

		#endregion
	}
}
